import React, { Component } from 'react';
import { connect } from 'react-redux';
import {
  increment,
  decrement,
  incrementAsync
} from '../../redux/actions/count';
class Count extends Component {
  increment = () => {
    const { value } = this.countNode
    this.props.increment(value * 1)
  }
  decrement = () => {
    const { value } = this.countNode
    this.props.decrement(value * 1)
  }
  incrementIfOdd = () => {
    const { value } = this.countNode
    if (this.props.count % 2 !== 0) {
      this.props.increment(value * 1)
    }
  }
  incrementAsync = () => {
    const { value } = this.countNode
    this.props.incrementAsync(value * 1, 500)
  }
  render() {
    return (
      <div>
        <h1>我是Count组件,下方person总数为{this.props.personNum}</h1>
        <h4>当前求和为：{this.props.count}</h4>
        <select ref={c => this.countNode = c}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>&nbsp;
        <button onClick={this.increment}>+</button>&nbsp;
        <button onClick={this.decrement}>-</button>&nbsp;
        <button onClick={this.incrementIfOdd}>odd+</button>&nbsp;
        <button onClick={this.incrementAsync}>addAsync</button>
      </div>
    )
  }
}
export default connect(
  state => ({
    count: state.count,
    personNum: state.persons.length
  }),
  {
    increment,
    decrement,
    incrementAsync
  }
)(Count)